/**
 *
 */
package org.cmcc.pcap.packet.util;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import java.util.Properties;

/**
 * @Date 2018年3月26日 下午6:04:46
 * @author Amish
 *
 *把applicationContext配置文件中的PropertyPlaceholderConfigurer改成PropertiesConfigUtils
 *即可实现原有PropertyPlaceholderConfigurer的功能，支持配置${pname}
 *也可实现过PropertiesConfigUtils直接获取Property List中的所有properties中配置的key：value
 */
public class PropertiesConfigUtils extends PropertyPlaceholderConfigurer {

    private static Properties properties;

    @Override  
    protected void processProperties(ConfigurableListableBeanFactory beanFactory,
                                     Properties props)throws BeansException {
  
        super.processProperties(beanFactory, props);  
        
        properties = new Properties();
        for (Object key : props.keySet()) {  
            String keyStr = key.toString();  
            String value = props.getProperty(keyStr);  

            properties.setProperty(keyStr, value);
        }  
    }  
  
 	//static method for accessing context properties  
 	public static Object getProperty(String key) {  
 		return properties.get(key);
 	} 

	/**
	 * only for test
	 * @param args
	 */
	public static void main(String[] args){
		ApplicationContext ac = new FileSystemXmlApplicationContext("classpath:config/applicationContextCustomized.xml");//WEB环境下这个applicationContext由web.xml配置加载
		//ac.getBean("propertyConfigurer");
		
		System.out.println(PropertiesConfigUtils.getProperty("transport.protocol"));
		System.out.println(PropertiesConfigUtils.getProperty("application.protocol"));
		System.out.println(PropertiesConfigUtils.getProperty("thread.concurrent"));
		System.out.println(PropertiesConfigUtils.getProperty("http.overtime"));
		System.out.println(PropertiesConfigUtils.getProperty("user.dir"));//null
		System.out.println(PropertiesConfigUtils.getProperty("java.vm.version"));//null
	}
}
